<?php

namespace App\Controllers\v1;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use common\utils\SysCode;
use App\Controllers\BaseController;
use App\Services\UserService;
use App\Events\UserLogin;
use App\User;
use App\Services\UserPropertyService;
use App\Services\BankService;
use App\Services\MoneyService;

class PropertyController extends BaseController
{

    public function info(Request $request)
    {
        $user = Auth::user();
        return UserPropertyService::info($user->id);
    }

    public function modify(Request $request)
    {
        $user = Auth::user();
        $attrs = $request->all();
        return UserPropertyService::modify($user->id, $attrs);
    }

    public function bankList(Request $request)
    {
        $user = Auth::user();
        $status = $request->input('status', 1);
        return BankService::userBank($user->id, $status);
    }

    public function banks(Request $request)
    {
        return BankService::all();
    }

    public function addBank(Request $request)
    {
        $this->validate($request, [
            'bank_id' => 'required',
            'name' => 'required',
            'card' => 'required',
            'address' => 'required',
            'withdraw_password' => 'required',
        ]);
        $user = Auth::user();
        $password = $request->input('withdraw_password');
        if (!MoneyService::validPassword($user->id, $password)) {
            return error_exit(SysCode::USER_WITHDRAW_PASSWORD_ERROR);
        }
        $params = $request->all(['bank_id', 'name', 'card', 'address']);
        return BankService::addUserBank($user->id, $params);
    }

    public function delBank(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'withdraw_password' => 'required',
        ]);
        $user = Auth::user();
        $id = $request->input('id');
        $password = $request->input('withdraw_password');
        if (!MoneyService::validPassword($user->id, $password)) {
            return error_exit(SysCode::USER_WITHDRAW_PASSWORD_ERROR);
        }
        return BankService::delUserBank($user->id, $id);
    }
}
